package com.example.administrator.chinafood;

import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;


public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener{

    private RadioGroup rg_bottom_bar;
    private RadioButton rb_check;
    private Fragment f1,f2,f3,f4,f5;

    private FragmentManager fManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        fManager = getFragmentManager();
        rg_bottom_bar = (RadioGroup)findViewById(R.id.rg_bottom_bar);
        rb_check = (RadioButton)findViewById(R.id.rb_menu_homepage);

        rg_bottom_bar.setOnCheckedChangeListener(this);
        rb_check.setChecked(true);
    }
    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        FragmentTransaction fragmentTransaction = fManager.beginTransaction();
        hideAllFragment(fragmentTransaction);
        switch(checkedId){
            case R.id.rb_menu_homepage:
                if (f1 == null) {
                    f1 = new FlHomepage();
                    fragmentTransaction.add(R.id.fl, f1);
                } else{
                    fragmentTransaction.show(f1);
                }
                break;
            case R.id.rb_menu_food:
                if (f2 == null) {
                    f2 = new FlFood();
                    fragmentTransaction.add(R.id.fl, f2);
                } else{
                    fragmentTransaction.show(f2);
                }
                break;
            case R.id.rb_menu_forum:
                if (f3 == null) {
                    f3 = new FlForum();
                    fragmentTransaction.add(R.id.fl, f3);
                } else{
                    fragmentTransaction.show(f3);
                }
                break;
            case R.id.rb_menu_more:
                if (f5 == null) {
                    f5 = new FlMore();
                    fragmentTransaction.add(R.id.fl, f5);
                } else{
                    fragmentTransaction.show(f5);
                }
                break;
            case R.id.rb_menu_person:
                if (f4 == null) {
                    f4 = new flPerson();
                    fragmentTransaction.add(R.id.fl, f4);
                } else{
                    fragmentTransaction.show(f4);
                }
                break;
        }
        fragmentTransaction.commit();
    }

    private void hideAllFragment(FragmentTransaction fragmentTransaction){
        if(f1 != null)fragmentTransaction.hide(f1);
        if(f2 != null)fragmentTransaction.hide(f2);
        if(f3 != null)fragmentTransaction.hide(f3);
        if(f4 != null)fragmentTransaction.hide(f4);
        if(f5 != null)fragmentTransaction.hide(f5);

    }
}
